﻿using System.Windows;
using System.Windows.Media.Animation;

namespace Comarch.Animation
{
    public class EntranceEffects
    {
        internal protected EntranceEffects(StoryboardBuilder storyboardBuilder)
        {
            StoryboardBuilder = storyboardBuilder;
        }
        public StoryboardBuilder StoryboardBuilder { get; private set; }

        public StoryboardBuilder Bounce(FrameworkElement element)
        {
            //TODO implement
            return StoryboardBuilder;
        }

        public StoryboardBuilder FlyIn(FrameworkElement element, int duration = 300, FlyDirection direction = FlyDirection.Left, IEasingFunction easingFunction = null)
        {
            var translate = TranslationCalculator.CalculateTranslationPoint(direction, element);
            easingFunction = easingFunction ?? new CubicEase();
            return StoryboardBuilder.Translate(element, (int)-translate.X, (int)-translate.Y, 0).Show(element)
                .Translate(element, (int)translate.X, (int)translate.Y, duration, easingFunction);
        }

        public  StoryboardBuilder FadeIn(FrameworkElement element, int duration = 1200, IEasingFunction easingFunction = null)
        {
            easingFunction = easingFunction ?? new CubicEase { EasingMode = EasingMode.EaseOut };
            return StoryboardBuilder
                .Hide(element, 0)
                .Show(element, duration * 2 / 3, easingFunction);
        }

        public  StoryboardBuilder OffsetFadeIn(FrameworkElement element, int duration, FlyDirection direction = FlyDirection.Left, int offset = 80)
        {
            var xOffset = TranslationCalculator.CalculateInitialXOffset(offset, direction);
            var yOffset = TranslationCalculator.CalculateInitialYOffset(offset, direction);

            return StoryboardBuilder
                .Hide(element, 0)
                .Translate(element, xOffset, yOffset, 0)
                .Show(element, duration * 2 / 3)
                .Translate(element, -xOffset, -yOffset, duration, new CubicEase { EasingMode = EasingMode.EaseOut });
        }

        public  StoryboardBuilder AddWidthEntry(FrameworkElement element, int duration = 300, IEasingFunction easingFunction = null)
        {
            var destValue = element.ActualWidth;
            element.Width = 0;
            element.Opacity = 1;
            easingFunction = easingFunction ?? new CubicEase { EasingMode = EasingMode.EaseOut };
            StoryboardBuilder.AddDoubleKeyFrames(element, "Width", destValue, duration, easingFunction, false);
            return StoryboardBuilder;
        }

        public  StoryboardBuilder AddHeightEntry(FrameworkElement element, int duration = 300, IEasingFunction easingFunction = null)
        {
            var destValue = element.ActualHeight;
            element.Height = 0;
            element.Opacity = 1;
            easingFunction = easingFunction ?? new CubicEase { EasingMode = EasingMode.EaseOut };
            StoryboardBuilder.AddDoubleKeyFrames(element, "Height", destValue, duration, easingFunction, false);
            return StoryboardBuilder;
        }
    }
}